'''
Created on 20 juil. 2011

@author: nathanael
'''



class Results(object):
    "Object store all results"
    __slots__ = ('values')
    def __init__(self, values=[]):
        self.values = values
    
    def __iter__(self):
        return iter(self.values)
    
    def append(self, result):
        self.values.append(result)
        
    def extend(self, results):
        self.values.extend(results)
        
    def clear(self):
        self.values = []
        return self
    
    def __len__(self):
        return len(self.values)
    
    

class Result(object):
    def get_xpath(self):
        return ""
    def get_str(self):
        return ""
    def __str__(self):
        return "<%s %s>" % (self.__class__.__name__, self.get_str())

class XResult(object):
    "Define elemental result"
    __slots__ = ('element', 'xpath', 'ref', 'regex')
    def __init__(self, element, xpath, regex, ref=None):
        self.element = element
        self.regex = regex
        self.xpath = xpath
        self.ref = ref
        
    def get_str(self):
        return "%s{%s}" % (self.xpath, str(self.regex))
    def get_xpath(self):
        return self.xpath


class ResultsFactory(object):
    def new_result(self, element, xpath, regex, ref):
        "build a new result."
        return XResult(element, xpath, regex, ref)

    def new_results(self, *argv, **kw):
        "build a new results container."
        return Results(*argv, **kw)
